<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<link rel="stylesheet" href="/css/font.css">
		<link rel="stylesheet" href="/css/xadmin.css">
		<script type="text/javascript" src="/js/jquery.min.js"></script>
		<script type="text/javascript" src="/lib/layui/layui.js" charset="utf-8"></script>
		<script type="text/javascript" src="/js/xadmin.js"></script>
		<style>
			form {
				margin-top: 10px;
			}
			
		</style>
	</head>
	<body>
		<form class="layui-form" id="saveMessageForm">
			<div class="layui-form-item">
				<label class="layui-form-label"><font color="red" size="3">*</font>标题：</label>
				<div class="layui-input-block">
					<input type="text" name="title" required lay-verify="required|title" placeholder="1-20个字符，且不能出现空格" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
                <label class="layui-form-label"><font color="red" size="3">*</font>消息类型：</label>
                <div class="layui-input-block">
			      <input type="radio" name="type" value="1" title="学校" checked>
			      <input type="radio" name="type" value="2" title="教师">
			      <input type="radio" name="type" value="3" title="学生">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><font color="red" size="3">*</font>置顶：</label>
                <div class="layui-input-block">
                    <input type="radio" name="top" value="1" title="是">
                    <input type="radio" name="top" value="0" title="否" checked>
                </div>
            </div>
            <div class="layui-form-item">
			    <label class="layui-form-label"><font color="red" size="3">*</font>消息内容：</label>
			    <div class="layui-input-block">
			     <textarea id="text" style="display: none;" name="txt" required lay-verify="txt"></textarea>
			    </div>
			  </div>
			<div class="layui-form-item">
				<div class="layui-input-block">
					<button class="layui-btn" lay-submit lay-filter="formDemo" type="button">立即提交</button>
				</div>
			</div>
		</form>
		
		<script>
			layui.use(['form', 'layedit', 'laydate'], function(){
				var form = layui.form,layer = layui.layer,layedit = layui.layedit;
			   var index = layedit.build('text',{
				   height: 160, //设置编辑器高度
				   tool: ['strong', 'italic', 'underline', 'del', '|','left', 'center', 'right', '|', 'link', 'unlink', 'face']
			   }); //建立编辑器
				form.verify({
					title: [/^[\S]{1,20}$/,'1-20个字符，且不能出现空格'],
					txt: function(value) {
                        if(layedit.getContent(index).length <= 0) {
                            return "消息内容不能为空";
                        }
                    }
                });
			
				//监听提交
				form.on('submit(formDemo)', function(data){
					$.ajax({
	                    url : "/sys/message/saveMessage",
	                    type : "post",
	                    data : {
	                    	title: data.field.title,
	                    	type: data.field.type,
	                    	top: data.field.top,
	                    	txt: layedit.getContent(index)
	                    },
	                    success : function(result) {
	                        if (result.state) {
	                            layer.msg('保存成功！', {icon: 1,time: 1000}, function(){
									// 1.先得到当前iframe层的索引
									var index = parent.layer.getFrameIndex(window.name); 
									// 2.再执行关闭 
									parent.layer.close(index); 
									// 调用父页面的方法
									window.parent.updateData();
	                            });
	                        } else {
	                            layer.msg(""+result.msg, {icon: 5});
	                        }
	                    }
	                });
				});
			});
		</script>
	</body>
</html>
